package org.leon.framework;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.*;

public class Bootstrap {

    private int port;
    private Mapper mapper = new Mapper();


    private void init() {
        try {
            loadServerConfig();
            mapper.init();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void start() throws IOException {

        int corePoolSize = 10;
        int maximumPoolSize = 50;
        long keepAliveTime = 100L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler
        );

        ServerSocket serverSocket = new ServerSocket(port);

        System.out.println("Mini Tomcat Start Successfully....  " + port);

        while (true) {
            Socket socket = serverSocket.accept();
            RequestProcessor processor = new RequestProcessor(socket, mapper);
            threadPoolExecutor.execute(processor);
        }
    }

    private void loadServerConfig() {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("conf/server.xml");
        SAXReader reader = new SAXReader();

        try {
            Document document = reader.read(resourceAsStream);
            Element rootElement = document.getRootElement();

            Element serviceNode = (Element) rootElement.selectSingleNode("//Service");

            Element connector = (Element) serviceNode.selectSingleNode("Connector");
            String portVal = connector.attributeValue("port");
            port = Integer.valueOf(portVal).intValue();

            Element engine = (Element) serviceNode.selectSingleNode("Engine");
            List<Element> hostEle = engine.selectNodes("Host");
            for (Element ele : hostEle) {
                String name = ele.attributeValue("name");
                String appBase = ele.attributeValue("appBase");

                MappedHost host = new MappedHost(name, appBase);
                mapper.getHostMap().put(name, host);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.init();
            bootstrap.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
